<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RabbitMQ 消息持久化完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e293b;
            color: #e2e8f0;
            border-radius: 12px;
            overflow-x: auto;
        }
        
        .code-block code {
            font-family: 'Consolas', 'Monaco', monospace;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl font-bold mb-6 tracking-tight">RabbitMQ 消息持久化</h1>
            <p class="text-xl mb-8 opacity-90 max-w-3xl mx-auto">掌握消息队列的核心技术，确保系统高可用性与数据安全</p>
            <div class="flex justify-center space-x-8 text-sm">
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>数据安全保障</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-server mr-2"></i>
                    <span>系统高可用</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sync-alt mr-2"></i>
                    <span>故障自动恢复</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>RabbitMQ 中，确保消息持久化是为了保证消息在系统崩溃或重启后不会丢失。持久化可以确保消息数据在磁盘上得到保存，即使 RabbitMQ 服务器出现故障，也能恢复消息。要实现消息的持久化，需要在生产者、交换机、队列等多个方面进行配置。
            </p>
        </div>

        <!-- Architecture Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-2xl font-bold mb-6 text-gray-800 section-title">持久化架构概览</h2>
            <div class="mermaid">
                graph TB
                    A[生产者] -->|持久化消息| B[交换机]
                    B -->|路由| C[持久化队列]
                    C -->|存储| D[磁盘]
                    C -->|消费| E[消费者]
                    E -->|确认| C
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Key Features Grid -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-12">
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover text-center">
                <div class="feature-icon mx-auto">
                    <i class="fas fa-database"></i>
                </div>
                <h3 class="font-bold text-lg mb-2">队列持久化</h3>
                <p class="text-gray-600 text-sm">确保队列在服务器重启后依然存在</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover text-center">
                <div class="feature-icon mx-auto">
                    <i class="fas fa-envelope"></i>
                </div>
                <h3 class="font-bold text-lg mb-2">消息持久化</h3>
                <p class="text-gray-600 text-sm">消息保存到磁盘，防止数据丢失</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover text-center">
                <div class="feature-icon mx-auto">
                    <i class="fas fa-exchange-alt"></i>
                </div>
                <h3 class="font-bold text-lg mb-2">交换机持久化</h3>
                <p class="text-gray-600 text-sm">保证路由规则在重启后保持不变</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover text-center">
                <div class="feature-icon mx-auto">
                    <i class="fas fa-check-circle"></i>
                </div>
                <h3 class="font-bold text-lg mb-2">确认机制</h3>
                <p class="text-gray-600 text-sm">确保消息可靠传递与处理</p>
            </div>
        </div>

        <!-- Section 1: 持久化队列 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 section-title">
                <i class="fas fa-database text-purple-600 mr-3"></i>持久化队列
            </h2>
            <p class="text-lg mb-6 text-gray-700 leading-relaxed">
                队列持久化确保队列在 RabbitMQ 服务器重启后依然存在。要实现队列持久化，必须在声明队列时设置 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">durable</code> 参数为 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">true</code>。
            </p>
            
            <h3 class="text-xl font-semibold mb-4 text-gray-800">声明持久化队列</h3>
            <p class="mb-4 text-gray-600">在声明队列时，将 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">durable</code> 参数设置为 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">true</code>：</p>
            
            <div class="code-block p-6 mb-6">
                <code>channel.queueDeclare("myQueue", true, false, false, null);</code>
            </div>
            
            <div class="bg-gray-50 rounded-lg p-6">
                <h4 class="font-semibold mb-3 text-gray-800">参数说明：</h4>
                <div class="space-y-2">
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">队列名:</strong> <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">"myQueue"</code>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">durable:</strong> <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">true</code> <span class="text-gray-600">（队列持久化）</span>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">exclusive:</strong> <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">false</code> <span class="text-gray-600">（队列对所有连接可见）</span>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">autoDelete:</strong> <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded text-sm">false</code> <span class="text-gray-600">（队列不会在最后一个消费者断开后自动删除）</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Section 2: 持久化消息 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 section-title">
                <i class="fas fa-envelope text-purple-600 mr-3"></i>持久化消息
            </h2>
            <p class="text-lg mb-6 text-gray-700 leading-relaxed">
                消息持久化确保消息在发布到队列时会被保存到磁盘。要实现消息持久化，需要在发布消息时设置 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">MessageProperties</code> 的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">deliveryMode</code> 为 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">2</code>，表示消息将持久化。
            </p>
            
            <h3 class="text-xl font-semibold mb-4 text-gray-800">发布持久化消息</h3>
            <p class="mb-4 text-gray-600">使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">deliveryMode</code> 设置为 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">2</code> 来确保消息持久化：</p>
            
            <div class="code-block p-6">
                <pre><code>AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
    .deliveryMode(2) // 2表示消息持久化
    .build();
channel.basicPublish("myExchange", "myRoutingKey", properties, message.getBytes());</code></pre>
            </div>
        </div>

        <!-- Section 3: 持久化交换机 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 section-title">
                <i class="fas fa-exchange-alt text-purple-600 mr-3"></i>持久化交换机
            </h2>
            <p class="text-lg mb-6 text-gray-700 leading-relaxed">
                虽然 RabbitMQ 允许声明持久化交换机，但交换机的持久化本身不会影